package com.sgb.kjwl.view.ui.fragment.material;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.CommoditycenterFragmentBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BossEntity;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.activity.supplier3.SupplierActivity4;
import com.sgb.kjwl.view.ui.adapter.material.RecommendAdapter;
import com.sgb.kjwl.view.ui.adapter.material.ShopNeedAdapter;
import com.sgb.kjwl.view.ui.fragment.home.MaterialHomeFragment;
import com.sgb.kjwl.viewmodel.home.CommodityCenterModel;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static com.swgk.core.event.EventConstant.EVENT_PUBLISH_PRICE_CHECK;

/**
 * 作者:张磊
 * 日期:2021/1/7 0007
 * 说明:1.3.6 商品中间的UI
 */
@Deprecated
public class CommodityCenterFragment extends AppFragment {

    @Inject
    CommodityCenterModel mModel;

    private CommoditycenterFragmentBinding mBinding;
    private Double mLocCurrentLong;
    private Double mLocCurrentLat;
    private RecommendAdapter mRecommendAdapter;
    private ShopNeedAdapter mShopNeedAdapter;
    private List<MenuEntity> data;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static CommodityCenterFragment newInstance(List<MenuEntity> data){
        CommodityCenterFragment commodityCenterFragment = new CommodityCenterFragment();
        Bundle bundle =new Bundle();
        bundle.putSerializable("data", (Serializable) data);
        commodityCenterFragment.setArguments(bundle);
        return commodityCenterFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.commoditycenter_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initListener() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        MaterialHomeFragment materialHomeFragment = (MaterialHomeFragment) getParentFragment();
        mBinding.llMore.setOnClickListener(v -> {
            UiUtil.sendMsg(EventConstant.EVENT_SKIP_MAP,false);
            //materialHomeFragment.startForProvider();
        });
        mBinding.ivMap.setOnClickListener(v -> {
            SupplierActivity4.start(getActivity(),true, data);
        });

        //ShopInfoActivity.start(getActivity(), mRecommendAdapter.getData().get(position).getId())
        mRecommendAdapter.setOnItemClickListener((adapter, view, position) -> ActivityJump.jumpToActivity(getContext(), ShopInfoForCompanyTradesActivity.class, Pair.create("id",mRecommendAdapter.getData().get(position).getId())));
        mShopNeedAdapter.setOnItemClickListener((adapter, view, position) -> mModel.showUrl(mShopNeedAdapter.getData().get(position).getEnquiryNo()));
    }

    private void initData() {
        data= (List<MenuEntity>) getArguments().getSerializable("data");
        mModel.getLocationLL();
        mModel.getDemandHallList();
    }

    private void initView() {
        mRecommendAdapter = new RecommendAdapter(null);
        mBinding.rvRecommend.setLayoutManager(new GridLayoutManager(UiUtil.getContext(), 2));
        mBinding.rvRecommend.setAdapter(mRecommendAdapter);
        mShopNeedAdapter = new ShopNeedAdapter(null);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(UiUtil.getContext(), 1));
        mBinding.rvList.setAdapter(mShopNeedAdapter);
    }

    public void showOptionView(List<BossEntity> data) {
        mRecommendAdapter.setNewData(data.size() > 4 ? data.subList(0, 4) : data);
    }

    public void getLocationLL(MapLongAndLatEntity data) {
        if (null != data) {
            mLocCurrentLong = Double.valueOf(data.getLng());
            mLocCurrentLat = Double.valueOf(data.getLat());
        }
        mModel.getRecommandSupplier(mLocCurrentLong, mLocCurrentLat);
    }

    public void setDemandHallList(List<DemandHallListEntity> data) {
        mShopNeedAdapter.setNewData(data);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onAttrEvent(LeaseEvent event) {
        if (event.getMessageTag() == EVENT_PUBLISH_PRICE_CHECK) {
            mModel.getDemandHallList();
        }
    }

}